﻿#include "mrwidget.h"
#include "ui_mrwidget.h"

#include <QTableWidgetSelectionRange>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextDocument>
#include <QTextBlock>
int MRWidget::identification=0;
MRWidget::MRWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MRWidget)
{
    ui->setupUi(this);
    MaterialReceive();
}

MRWidget::~MRWidget()
{
    delete ui;
}

void MRWidget::MaterialReceive()
{
    ui->MRstartdE->setDate(QDate::currentDate());
    ui->MRenddE->setDate(QDate::currentDate());
    ui->MRstartdE->setEnabled(false);
    ui->MRenddE->setEnabled(false);
    //设置日期控件相关
    ui->MRtw->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->MRtw->hideColumn(0);
    ui->MRtw->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->MRtw->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    //设置接收材料表
    connect(ui->MRtw->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(MRshowsort(int)));
    QSqlQuery query;
    query.exec("select *from ZZB.MaterialReceive limit 20");
    while(query.next())
{
        ui->MRtw->insertRow(0);
        QString mr0=query.value(0).toString();
        ui->MRtw->setItem(0,0,new QTableWidgetItem(mr0));

        QString mr1=query.value(1).toString();
        ui->MRtw->setItem(0,1,new QTableWidgetItem(mr1));

        QString mr2=query.value(2).toString();
        ui->MRtw->setItem(0,2,new QTableWidgetItem(mr2));

        QString mr3=query.value(3).toString();
        ui->MRtw->setItem(0,3,new QTableWidgetItem(mr3));

        QString mr4=query.value(4).toString();
        ui->MRtw->setItem(0,4,new QTableWidgetItem(mr4));

        QString mr5=query.value(5).toString();
        ui->MRtw->setItem(0,5,new QTableWidgetItem(mr5));
    }
}

void MRWidget::on_MReditbtn_clicked()
{
    int i=ui->MRtw->currentRow();
    if(i>-1){
    MReditDialog mredit(this);
    connect(this,SIGNAL(sendData(QStringList)),&mredit,SLOT(receiveData(QStringList)));
    QList<QTableWidgetItem*>items=ui->MRtw->selectedItems();
    QString str = ui->MRtw->item(i,0)->text();
       QString date=items.at(0)->text();

       QString unit=items.at(1)->text();
       QString envelope=items.at(2)->text();
       QString copies=items.at(3)->text();
       QString content=items.at(4)->text();
       QStringList fan;
       fan<<str<<date<<unit<<envelope<<copies<<content;
    emit sendData(fan);
    mredit.exec();
    //delete mredit;
    }
    else{
    QMessageBox::information(this,"提示信息","未选中编辑行!");
    }
    on_MRseekbtn_clicked();
}

void MRWidget::MRshowdatabase()
{//显示接收表全部信息，添加函数也有调用
    ui->MRtw->clearContents();
    ui->MRtw->setRowCount(0);
    QSqlQuery query;
    query.exec("select *from ZZB.MaterialReceive");
    while(query.next())
{
        ui->MRtw->insertRow(0);
        QString mr0=query.value(0).toString();
        ui->MRtw->setItem(0,0,new QTableWidgetItem(mr0));

        QString mr1=query.value(1).toString();
        ui->MRtw->setItem(0,1,new QTableWidgetItem(mr1));

        QString mr2=query.value(2).toString();
        ui->MRtw->setItem(0,2,new QTableWidgetItem(mr2));

        QString mr3=query.value(3).toString();
        ui->MRtw->setItem(0,3,new QTableWidgetItem(mr3));

        QString mr4=query.value(4).toString();
        ui->MRtw->setItem(0,4,new QTableWidgetItem(mr4));

        QString mr5=query.value(5).toString();
        ui->MRtw->setItem(0,5,new QTableWidgetItem(mr5));
    }
}

void MRWidget::on_MRseekbtn_clicked()
{
    QString select;
    if(ui->MRtw->currentRow()>-1)
        select=ui->MRtw->item(ui->MRtw->currentRow(),0)->text();
    //select是为了寻找接收表中最开始的选中行，同时在编辑和删除中引入的是查询的函数
    ui->MRtw->clearContents();
    ui->MRtw->setRowCount(0);
    QString Unititem=ui->MRunitlE->text();
    QString Contentitem=ui->MRcontentlE->text();

    bool unitok=Unititem.isEmpty();
    bool contentok=Contentitem.isEmpty();
    bool startok=ui->MRstartCB->isChecked();
    bool endok=ui->MRendCB->isChecked();
    //一系列判断条件
    QString unitstr;
    QString contentstr;
    QString startstr;
    QString endstr;
    QString str;
    int i=0;

if(!unitok){
    unitstr=QString(" WHERE OriginalUnit='%1'").arg(Unititem);
i=1;}
if(!contentok){
    if(i==0){contentstr=QString(" WHERE MaterialContent LIKE '%%1%' ").arg(Contentitem);
    i=1;}
    else{contentstr=QString(" AND MaterialContent LIKE '%%1%' ").arg(Contentitem);}}
if(startok){
    if(i==0){startstr=QString(" WHERE ReceiveDate>='%1'").arg(ui->MRstartdE->text());
        i=1;}
    else{startstr=QString(" AND ReceiveDate>='%1'").arg(ui->MRstartdE->text());}}
if(endok){
    if(i==0){endstr=QString(" WHERE ReceiveDate<='%1'").arg(ui->MRenddE->text());
    i=1;}
    else{endstr=QString(" AND ReceiveDate<='%1'").arg(ui->MRenddE->text());}}
str=QString("SELECT *FROM ZZB.MaterialReceive"+unitstr+contentstr+startstr+endstr);
//拼接查询语句
    QSqlQuery query;
 if(query.exec(str)){qDebug()<<"成功查询";}
 else{qDebug()<<"失败";}

    while(query.next())
    {
     ui->MRtw->insertRow(0);
     QString mr0=query.value(0).toString();
     ui->MRtw->setItem(0,0,new QTableWidgetItem(mr0));

     QString mr1=query.value(1).toString();
     ui->MRtw->setItem(0,1,new QTableWidgetItem(mr1));

     QString mr2=query.value(2).toString();
     ui->MRtw->setItem(0,2,new QTableWidgetItem(mr2));

     QString mr3=query.value(3).toString();
     ui->MRtw->setItem(0,3,new QTableWidgetItem(mr3));

     QString mr4=query.value(4).toString();
     ui->MRtw->setItem(0,4,new QTableWidgetItem(mr4));

     QString mr5=query.value(5).toString();
     ui->MRtw->setItem(0,5,new QTableWidgetItem(mr5));
    }
    if(select.toInt()>0){
        for(int j=0;j<ui->MRtw->rowCount();j++)
            {
           if(ui->MRtw->item(j,0)->text().toInt()==select.toInt())
               ui->MRtw->selectRow(j);
        }
    }//编辑之后，调用的是查找函数，在这里默认选中编辑行并显示相关的材料内容
}


void MRWidget::on_MRdeletebtn_clicked()
{
    QList<QTableWidgetItem*> itemsTemp = ui->MRtw->selectedItems();
    int countTemp = itemsTemp.count();
    if(countTemp == 0){//如果未选中记录
        QMessageBox::information(this,"提示信息","请选中删除行!");
    }else{
        ui->MRtw->showColumn(0);//取id前使隐藏列可见

        QList<QTableWidgetSelectionRange> ranges = ui->MRtw->selectedRanges();
        QStringList list;//存放id
        int countRow = ranges.count();
        for(int i=0 ; i < countRow ; i++)
        {
           int topRow=ranges.at(i).topRow();
           int bottomRow=ranges.at(i).bottomRow();
           for(int j=topRow;j<=bottomRow;j++)
           {
              list.append(ui->MRtw->item(j,0)->text());
            }
        }
        ui->MRtw->setColumnHidden(0, true);//取完id后继续隐藏id
        QSqlQuery query;
        QMessageBox::StandardButton btnDel;
        btnDel = QMessageBox::question(this, "提示", "确定删除吗?", QMessageBox::Yes|QMessageBox::No);
        if (btnDel == QMessageBox::Yes) {
            for(int i = 0; i< list.size();i++)
            {
                QString strItem = list.at(i);
                QString strSql = QString("DELETE FROM ZZB.MaterialReceive WHERE ReceiveID='%1';").arg(strItem);
                bool delFlag = query.exec(strSql);
                if(delFlag == false){
                    QMessageBox::information(this,"提示信息","删除失败!");
                }
            }
        on_MRseekbtn_clicked();
        ui->MRtw->selectRow(-1);
        }
    }
}

void MRWidget::on_MRaddbtn_clicked()
{//添加，每个添加的都会立刻显示
    MRaddDialog mradd(this);
    mradd.exec();
}

void MRWidget::on_MRendCB_clicked()
{
    bool ok;
    ok=ui->MRenddE->isEnabled();
    if(ok){ui->MRenddE->setEnabled(false);}
    else
    {ui->MRenddE->setEnabled(true);}
}

void MRWidget::on_MRstartCB_clicked()
{
    bool ok;
    ok=ui->MRstartdE->isEnabled();
    if(ok){ui->MRstartdE->setEnabled(false);}
    else
    {ui->MRstartdE->setEnabled(true);}
}

void MRWidget::on_MRpreviewbtn_clicked()
{
    int i=ui->MRtw->currentRow();
    if(i>-1){
    QString filePth = QCoreApplication::applicationDirPath() + "/";
        QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), filePth, "*.pdf");
        if (!fileName.isEmpty())
        {
            // 如果文件后缀为空，则默认使用.pdf
            if (QFileInfo(fileName).suffix().isEmpty())
            {
                fileName.append(".pdf");
            }
            QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
            // 指定输出格式为pdf
            printer->setPageSize(QPrinter::A4);
            printer->setOutputFormat(QPrinter::PdfFormat);
            printer->setOutputFileName(fileName);
            QTextDocument text_document;
            text_document.setHtml(MRsaveHtmlToPDF(fileName));
            text_document.print(printer);
            text_document.end();
}
    }else{QMessageBox::information(this,"提示信息","未选择打印条目行!");}
}

QString MRWidget::MRsaveHtmlToPDF(QString Q){
    //生成HTML，之后按形式导出pdf
    QString html = "";
    html += "<html>";
    html += "<head>";
    html += "<title>"+Q+"</title>";
    html += "<head>";
    html += "<body>";
    html += "<h1>材料接收信息</h1>";
    html += "<font size=""5"" face=""arial"">";
    QList<QTableWidgetSelectionRange> ranges = ui->MRtw->selectedRanges();
    int countTemp = ranges.count();
    if(countTemp==1){
    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
    html += "<tr>";
        html += "<th colspan=\"2\">材料接收表</th>";
    html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">接收日期</td>";
            html += "<td align=\"center\">"+ui->MRtw->item(ui->MRtw->currentRow(),1)->text()+"</td>";
        html += "</tr>";

        html += "<tr>";
            html += "<td align=\"center\">转来单位</td>";
            html += "<td align=\"center\">"+ui->MRtw->item(ui->MRtw->currentRow(),2)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">原信封号</td>";
            html += "<td align=\"center\">"+ui->MRtw->item(ui->MRtw->currentRow(),3)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">接收份数</td>";
            html += "<td align=\"center\">"+ui->MRtw->item(ui->MRtw->currentRow(),4)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">材料内容</td>";
            html += "<td align=\"center\">"+ui->MRtw->item(ui->MRtw->currentRow(),5)->text()+"</td>";
        html += "</tr>";
        html += "</font>";
    html += "</table>";}
    else{
        html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
        html += "<tr>";
            html += "<th colspan=\"5\">材料接收表</th>";
        html += "</tr>";
            html += "<tr>";
                html += "<td align=\"center\">接收日期</td>";
                html += "<td align=\"center\">转来单位</td>";
                html += "<td align=\"center\">原信封号</td>";
                html += "<td align=\"center\">接收份数</td>";
                html += "<td align=\"center\">材料内容</td>";
            html += "</tr>";
        for(int i=0;i<countTemp;i++){
               int topRow=ranges.at(i).topRow();
               int bottomRow=ranges.at(i).bottomRow();
               for(int j=topRow;j<=bottomRow;j++)
               {
                   html += "<tr>";
                   html += "<td align=\"center\">"+ui->MRtw->item(j,1)->text()+"</td>";
                   html += "<td align=\"center\">"+ui->MRtw->item(j,2)->text()+"</td>";
                   html += "<td align=\"center\">"+ui->MRtw->item(j,3)->text()+"</td>";
                   html += "<td align=\"center\">"+ui->MRtw->item(j,4)->text()+"</td>";
                   html += "<td align=\"center\">"+ui->MRtw->item(j,5)->text()+"</td>";
                   html += "</tr>";
                }
            }

        html += "</font>";
    html += "</table>";
    }
    html += "</body>";
    html += "</html>";
return html;
}

void MRWidget::MRshowsort(int clounm)
{//排序
    if(identification==1){
    ui->MRtw->sortItems(clounm,Qt::DescendingOrder);
    identification=0;}
    else{ui->MRtw->sortItems(clounm,Qt::AscendingOrder);
    identification=1;}
}
